<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商品 - 出售账号的')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: summernote-css" />
    <style>
        .btn-outline-secondary{
            display: none;
        }
        .note-insert{
            display: none;
        }
        .modal-backdrop{position: relative}
    </style>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-account-edit" th:object="${gameAccount}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">游戏名称：</label>
                <div class="col-sm-8">
                    <select name="gameTypeId" id="gameTypeId" class="form-control m-b">
                        <option value="">--请选择游戏--</option>
                        <option th:each="game : ${gameList}" th:selected="${game.id == gameAccount.gameTypeId}"  th:text="${game.gname}" th:value="${game.id}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">是否显示：</label>
                <div class="col-sm-8">
                    <select name="showStatus" class="form-control m-b" th:with="type=${@dict.getType('sys_show_hide')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{showStatus}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">编号：</label>
                <div class="col-sm-8">
                    <input name="accountNo" th:field="*{accountNo}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">帐号：</label>
                <div class="col-sm-8">
                    <input name="gameNo" class="form-control" type="text" th:field="*{gameNo}" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <textarea name="title" class="form-control" required>[[*{title}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">价格：</label>
                <div class="col-sm-8">
                    <input name="price" th:field="*{price}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">详情：</label>
                <div class="col-sm-8">
                    <input type="hidden" id="detail-html" name="detail" value="">
                    <div class="ibox float-e-margins">
                        <div class="ibox-title">
                            <h5>详情介绍</h5>
                        </div>
                        <div class="ibox-content no-padding">
                            <div class="summernote">[[*{detail}]]</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图片列表：</label>
                <div class="col-sm-8">
                    <div id="img-list">
                        <img th:each="array : ${#strings.arraySplit(gameAccount.imgs, ',')}" th:src="${array}" width="150" height="90" alt="" style="float: left;margin-right: 10px;">
                    </div>
                    <div>
                        <button type="button" class="btn btn-sm btn-primary" id="update-img-btn1" style="margin-bottom: 10px" onclick="updateImgBtn('1')">
                            <i class="fa fa-edit"></i>修改图片</button>
                        <button type="button" id="update-img-btn" class="btn btn-sm btn-primary" style="display: none" onclick="updateImgBtn('2')">
                            <i class="fa fa-edit"></i>关闭修改</button>
                    </div>
                </div>
            </div>
            <div class="form-group" style="display: none;" id="up-div">
                <label class="col-sm-3 control-label is-required">图片上传：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="imgs" id="imgValue" th:field="*{imgs}"/>
                    <input id="imgs" name="files" type="file" multiple>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">搜索条件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="searchParam" id="search-param" th:field="*{searchParam}" class="form-control" value="" />
                    <input id="searchParam" class="form-control" value="" th:field="*{searchParam}" disabled />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sortNum" class="form-control" type="text" th:field="*{sortNum}">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <button type="button" class="btn btn-sm btn-primary" onclick="setcondition()">设置条件</button>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: summernote-js" />
    <script th:inline="javascript">
        $.fn.serializeObject = function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        };
        var prefix = ctx + "system/account";
        $("#form-account-edit").validate({
            rules:{
                gameTypeId:{
                    required: true
                }
            },
            focusCleanup: true
        });

        //富文本编辑
        $(document).ready(function () {
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                callbacks: {
                    onImageUpload: function (files, editor, $editable) {
                        sendFile(files);
                    }
                }
            });
            //给富文本赋值
            $('.summernote').summernote('code', [[${gameAccount.detail}]]);
        });

        function sendFile(files, editor, $editable) {
            var data = new FormData();
            data.append("file", files[0]);
            $.ajax({
                data : data,
                type : "POST",
                url : ctx + 'common/qiniu/upload',
                cache : false,
                contentType : false,
                processData : false,
                dataType : "json",
                success: function(res) {
                    $('.summernote').summernote('insertImage', res.url,res.fileName);
                },
                error:function(){
                    $.modal.alertError('上传失败')
                }
            });
        }

        // 多图上传
        $("#imgs").fileinput({
            uploadUrl: ctx + 'common/qiniu/uploads',
            uploadAsync: false
        }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
            var rsp = data.response;
            $("#imgValue").val(rsp.urls);
            var arr = rsp.urls;
            var imghtml = '';
            for(var i=0;i<arr.length;i++){
                imghtml += '<img  src="'+arr[i]+'" width="150" height="90" alt="" style="float: left;margin-right: 10px;">'
            }
            $('#img-list').html(imghtml);
            updateImgBtn('2');
            log.info("return urls：" + rsp.urls)
            log.info("reutrn fileNames：" + rsp.fileNames)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })

        function updateImgBtn(type) {
            if(type ==  '1'){
              $("#up-div").show();
              $("#update-img-btn1").hide();
              $("#update-img-btn").show();
            }else{
              $("#up-div").hide();
              $("#update-img-btn").hide();
              $("#update-img-btn1").show();
            }
        }

        //设置条件
        function setcondition() {
            var typeid = $("#gameTypeId option:selected").val();
            var text = $("#gameTypeId option:selected").text();
            if(typeid == ''){
                $.modal.alertError('请选择游戏分类!');  return
            }
            var options = {
                title: text + '- 条件设置',
                width: '800',
                height: '600',
                url: prefix + "/setCodition/" + typeid,
                callBack: callback
            };
            $.modal.openOptions(options);
        }

        //设置条件回调
        function callback(index, layero) {
            var body = $.modal.getChildFrame(index);
            var results = [];
            body.find("input[type='radio']:checked").each(function(){
                var v = $(this).val();
                if(results.indexOf(v) == -1){
                    results.push($(this).val());
                }
            });
            body.find("input[type='checkbox']:checked").each(function(){
                var v = $(this).val();
                if(results.indexOf(v) == -1){
                    results.push($(this).val());
                }
            });
            $("#searchParam").val(results.join(","));
            $("#search-param").val(results.join(","));
            $.modal.close(index);
        }
        
        function submitHandler() {
            if ($.validate.form()) {
                var shtml =  $('.summernote').summernote('code');
                if(shtml == '<p><br></p>' || shtml== '') {
                    $.modal.alertError('账号详情必填');  return
                }
                $('#detail-html').val(shtml);
                console.log($('#form-account-edit').serializeObject().detail);
                $.operate.save(prefix + "/edit", $('#form-account-edit').serializeObject());
            }
        }
    </script>
</body>
</html>